package com.ss.summer.service.impl;

import com.ss.summer.core.exceptions.SummerSearchException;
import com.ss.summer.dao.SummerInstanceDao;
import com.ss.summer.sql.core.SummerQuery;
import com.ss.summer.sql.model.SummerApply;
import com.ss.summer.sql.model.SummerPage;
import com.ss.summer.service.SummerSearchPlus;
import com.ss.summer.service.enmus.HasAuthType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xia17
 * @date 2019/8/1 10:33
 */

@Transactional(rollbackFor = Exception.class)
@Service
public class SummerSearchPlusImpl implements SummerSearchPlus {

    private SummerInstanceDao summerInstanceDao;

    @Autowired
    public SummerSearchPlusImpl(SummerInstanceDao summerInstanceDao){
        this.summerInstanceDao = summerInstanceDao;
    }


    @Override
    public <T extends SummerApply> List<T> findWaitAudited(String apiPath, String memberId, SummerQuery query, Class<T> clz, HasAuthType type) {
        if (type == null){
            throw new SummerSearchException("type参数不能为null");
        }
        List<T> result ;
        switch (type){
            case ALL: result = summerInstanceDao.findHasAuth(apiPath,memberId,query,clz);break;
            case WAIT_AUDIT: result = summerInstanceDao.findWaitAudit(apiPath,memberId,query,clz);break;
            case AUDITED: result = summerInstanceDao.findAudited(apiPath,memberId,query,clz);break;
            case AUDITED_OTHER: result = summerInstanceDao.findAuditedForOther(apiPath,memberId,query,clz);break;
            default:throw new SummerSearchException("未匹配到对应HasAuthType枚举类型");
        }
        return result;
    }

    @Override
    public <T extends SummerApply> SummerPage<T> findWaitAudited(String apiPath, String memberId, SummerQuery query, Class<T> clz, HasAuthType type, int page, int size) {
        if (type == null){
            throw new SummerSearchException("type参数不能为null");
        }
        SummerPage<T> result;
        switch (type){
            case ALL: result = summerInstanceDao.findHasAuth(apiPath,memberId,query,clz,page,size);break;
            case WAIT_AUDIT: result = summerInstanceDao.findWaitAudit(apiPath,memberId,query,clz,page,size);break;
            case AUDITED: result = summerInstanceDao.findAudited(apiPath,memberId,query,clz,page,size);break;
            case AUDITED_OTHER: result = summerInstanceDao.findAuditedForOther(apiPath,memberId,query,clz,page,size);break;
            default:throw new SummerSearchException("未匹配到对应HasAuthType枚举类型");
        }
        return result;
    }
}
